<template>
  <div class="read m-center">
    <!-- 头部 -->
    <div class="article-header">
      <img class="article-icon" src="https://www.imooc.com/static/img/article/article-logo.png" alt="">
      <div class="search-container">
        <div class="search-box">
          <input v-model="query.title" type="text" @keyup.enter="getReadListData" placeholder="搜索感兴趣的知识和文章">
          <i class="iconfont" @click="getReadListData(title)">&#xe63c;</i>
        </div>
        <span class="write-btn">写文章</span>
      </div>
    </div>

    <!-- 列表 -->
    <div class="read-list">
      <ul>
        <li v-for="(item, index) in filterReadList" :key="index" class="read-item" @click="handleReadClick(item)">
          <div class="img-box">
            <img :src="item.img" alt="">
          </div>
          <div class="read-content">
            <p class="title">
              {{ item.title }}
            </p>
            <p class="read-desc">
              {{ item.des }}
            </p>
            <p class="author">
              <img :src="item.avatar" class="avatar" alt="">
              <span class="name">{{ item.author }}</span>
              <span class="split">/</span>
              <span class="job">{{ item.job }}</span>
            </p>
            <p class="other">
              <span v-if="item.type == 'vip'" class="vip">{{item.type}}</span>
              <span v-else class="free">{{item.type}}</span>
            </p>
          </div>
        </li>
      </ul>
    </div>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getReadListData"/>
  </div>
</template>
<script>
  import { mapGetters } from 'vuex'
import { getArticleList } from 'api/article.js'
import Pagination from 'components/pagination/pagination.vue'
import { ERR_OK } from 'api/config.js'
export default {
  components: {Pagination},
  data () {
    return {
      listQuery: {
        page: 1,
        limit: 20
      },
      total: 0,
      query: {},
      readList: [], // 专栏列表数据
      currentIndex: 0 // 当前分类的索引
    }
  },
  mounted () {
    this.getReadListData(this.title)
  },
  methods: {
    // 专栏点击事件
    handleReadClick (row) {
      if (this.userInfo.identity == undefined) {
        //未登录
        this.$router.push(`/read/${row.id}`)
      } else {
        //判断是否是vip身份
        if (row.type != '免费' && this.userInfo.identity != row.type) {
          this.$message.error('您还不是VIP身份，请联系管理员购买VIP身份')
          return;
        }
        this.$router.push(`/read/${row.id}`)
      }
    },
    // 获取专栏列表数据
    getReadListData () {
      getArticleList(this.query, this.listQuery).then(res => {
        let { code, data } = res
        if (code === ERR_OK) {
          this.readList = data.rows
          this.total = data.total
        }
      })
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
    // 专栏分类
    typeList () {
      let result = []
      let navSet = new Set(['全部'])
      this.readList.forEach(item => {
        navSet.add(item.type)
      })
      result = Array.from(navSet)
      return result
    },
    // filter后的专栏数据
    filterReadList () {
      let list = this.readList.slice()
      let currType = this.typeList[this.currentIndex]
      if (this.currentIndex !== 0) {
        list = list.filter(item => item.type === currType)
      }
      return list
    }
  }
}
</script>
<style lang="stylus" scoped>
  @import '~assets/stylus/variables.styl';
  @import '~assets/stylus/mixin.styl';
  .article-header
    margin-top: 10px;
    height: 60px;
    .article-icon
      display: inline-block;
      vertical-align: middle;
      height: 100%;
    .search-container
      float: right;
      padding: 12px 0;
      height: 60px;
      box-sizing: border-box;
      .search-box
        display: inline-block;
        position: relative;
        width: 480px;
        height: 36px;
        line-height: 36px;
        border-radius: 8px;
        background-color: $border-three-color;
        input
          padding: 0 60px 0 20px;
          width: 100%;
          height: 36px;
          box-sizing: border-box;
          line-height: 36px;
          outline: none;
          background: none;
          line-height: 0;
          font-size: 14px;
          color: $font-four-color;
        .iconfont
          position: absolute;
          top: 0;
          right: 20px;
          font-size: 20px;
          font-weight: 700;
          color: $font-four-color;
          cursor: pointer;
      .write-btn
        display: inline-block;
        margin-left: 10px;
        padding: 8px 24px;
        border-radius: 18px;
        background-color: $theme-blur-light-color;
        line-height: 18px;
        font-size: 14px;
        color: #fff;
        cursor: pointer;
  .read
    .read-nav
      dl
        position: relative;
        padding: 16px 0px 10px 52px;
        border-bottom: 1px solid rgba(7,17,27,0.1);
        font-size: 14px;
        dt
          position: absolute;
          left: 0;
          top: 23px;
          font-weight: 700;
        dd
          display: inline-block;
          vertical-align: middle;
          margin-right: 5px;
          margin-bottom: 10px;
          padding: 0 10px;
          line-height: 30px;
          cursor: pointer;
          &.active
            color: $theme-red-color;
            background-color: rgba(242,13,13,0.06);
            border-radius: 6px;
            font-weight: 700;
    .read-list
      .read-item
        display: flex;
        align-items: flex-start;
        margin: 50px 0;
        padding-bottom: 15px;
        border-bottom: 1px solid rgba(7,17,27,0.1);
        cursor: pointer;
        &:last-child
          border-bottom: none;
        &:hover
          .img-box
            & > img
              transform: scale(1.2);
          .read-content
            .title
              color: #6698ff;
        .img-box
          margin-right: 30px;
          img-box(138px, 157px)
          & > img
            transform: scale(1);
            transition: transform 0.2s ease-in-out;
            background-color: transparent;
            cursor: pointer;
        .read-content
          flex: 1;
          .title
            font-size: 20px;
            font-weight: 700;
            color: $font-first-color;
            line-height: 36px;
          .read-desc
            font-size: 14px;
            color: $font-second-color;
            line-height: 24px;
            font-weight: 700;
          .author
            margin: 8px 0;
            font-size: 12px;
            img, span
              display: inline-block;
              vertical-align: middle;
            .avatar
              margin-right: 10px;
              width: 36px;
              height: 36px;
              border-radius: 50%;
            .split
              padding: 0 10px;
          .try-read
            padding: 10px 0;
            .try-item
              display: inline-block;
              vertical-align: middle;
              margin-bottom: 12px;
              width: 33.33%;
              font-size: 12px;
              color: $font-second-color;
              line-height: 24px;
              cursor: pointer;
              &:hover
                color: $theme-blue-color;
              .icon
                margin-right: 10px;
                padding: 4px;
                background-color: rgba(102,152,255, 0.1);
                color: $theme-blue-color;
                font-weight: 700;
          .other
            font-size: 12px;
            line-height: 45px;
            color: $font-four-color;
            & > span
              display: inline-block;
              vertical-align: middle;
              padding: 0 5px;
              &.free
                left: -8px;
                top: 8px;
                padding: 0 8px;
                border-radius: 12px;
                border: 2px solid #fff;
                line-height: 20px;
                background-color: $theme-blue-color;
                font-size: 12px;
                font-weight: 700;
                color: #fff;
              &.vip
                left: -8px;
                top: 8px;
                padding: 0 8px;
                border-radius: 12px;
                border: 2px solid #fff;
                line-height: 20px;
                background-color: $theme-red-color;
                font-size: 12px;
                font-weight: 700;
                color: #fff;
</style>
